

/*------------------------------------------------------------------------------
	1. Housekeeping
------------------------------------------------------------------------------*/

	*Run housekeeping code
		qui do ".../replication_package/housekeeping.do"

	*Write log
		log using "$log_loc/figure_a3.log", text replace

/*------------------------------------------------------------------------------
	2. Clean Census Datasets
------------------------------------------------------------------------------*/

	*Import Census Datasets
		import excel using "$data/Census Count/nc-est2019-syasexn.xlsx", clear cellra(A6:M22)
			keep A K L M
			ren (A K L M) (age_num census_2017 census_2018 census_2019)

		tempfile census_data
		save `census_data', replace

		import excel using "$data/Census Count/nc-est2022-syasexn.xlsx", clear cellra(A6:E22)
			keep A C D
			ren (A C D ) (age_num census_2020 census_2021)

			merge 1:1 age_num using `census_data'
				assert _merge == 3
				drop _merge

			replace age_num = substr(age_num, 2, .)
			destring age_num, replace
			sort age_num

	*Make the Counts Per Year
		reshape long census_, i(age_num) j(year)
			ren census_ census_ct
			isid year age_num

		tempfile census_dta
		save `census_dta', replace

/*------------------------------------------------------------------------------
	3. Clean Main Datasets
------------------------------------------------------------------------------*/

		*Import Data
		use "$data/childrens2018.dta", clear

			/*Restrict Sample*/
				*Keep f1095 filings
					rename state_insurance state
					fmerge m:1 state using "$data/statelist.dta"
						assert _merge != 2 //Confirm that all 51 states are in dataset
						drop if _merge != 3
						drop _merge

				*Keep if age_num is within the range
					keep if age_num >= 0 & age_num <= 16

				*Keep if kid died after end of year or is currently alive
					keep if 2018*10000+1231<death_date|death_date==0

				*Keep if months covered is possible
					keep if num_cov_ins > 0 & num_cov_ins <= 12

			/*Generate variables for figures*/	
				*Generate outcame variable on claiming
					gen onreturn = file_inc != .

				*Generate a count variable
					gen ct = 1

				*Collapse to the by-age_num level
					collapse (sum) ct onreturn, by(age_num)

				*Merge in Census Data
					gen year = 2018
					merge 1:1 year age_num using `census_dta'
						assert _merge != 1
						keep if _merge == 3
						drop _merge

				*Adjust for claiming limits
					replace onreturn = onreturn * 1.0017029

				*Create Percent Claimed Upper Bound
					gen perc_claimed_ub = (onreturn/ct) * (ct/census_ct) + (1 - ct/census_ct)

				*Create Percent Claimed Lower Bound
					gen perc_claimed_lb = (onreturn/ct) * (ct/census_ct)

				*Generate outcome (i.e., percent of children claimed)
					gen perc_return = onreturn/ct
			
				*Adjust outcome to be out of 100
					replace perc_return = perc_return*100
					replace perc_claimed_ub = perc_claimed_ub * 100
					replace perc_claimed_lb = perc_claimed_lb * 100

/*------------------------------------------------------------------------------
	4. Create Figure 1
------------------------------------------------------------------------------*/

			/*Create Figures*/	
				*Create Graph
				twoway (rarea perc_claimed_ub perc_claimed_lb age_num, fcolor(gs13) lcolor(gs1)) || ///
					(connected perc_return age_num, msymbol(o) mlcolor(gs1) mfcolor(white) clcolor(gs1) lpattern(dash)) , ///
					ylabel(, format(%5.1f) labsize(small)) ///
					xtitle("Age", height(4)) ///
					xlabel(0(1)16, labsize(small)) ///	
					ytitle("Share Claimed (%)", size(medsmall)) ///
					legend(label(1 "Manski Bounds") label(2 "Average_num") rows(2) order (2 1) size(small) pos(0) bplace(seast) region(lcolor(black) lwidth(thin))) ///
					graphregion(fcolor(white) color(white) lcolor(white))


				*Export Graph
					graph save "$output/by age 2018 Manski Bounds.gph", replace 
